<template>
  <el-container class="home-container">
    <!-- 头部区 -->
    <el-header>
      <div>
        <img src="../../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏区 -->
      <el-aside width="200px">
        <!-- 点击按钮 -->
        <!-- <div class="toggle-button" @click="toggleCollapse">|||</div> -->
        <!-- 侧边栏菜单区 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 index也要动态绑定 数值和字符串拼接为数值-->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 显示菜单页动态绑定-->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 防止值重复，index加一个动态绑定 手动补全斜线/,前面没帮我们添加-->
            <el-menu-item :index="'/'+subItem.path + ''" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu></el-aside
      >
      <!-- 右侧内容体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data(){
    return{   //挂载到data中不会消失
      menulist:[],
      iconsObj:{
        '125':'el-icon-user-solid',
        '103':'iconfont el-icon-s-tools',
        '101':'iconfont el-icon-s-goods',
        '102':'iconfont el-icon-s-order',
        '145':'iconfont el-icon-s-data'
      },
      // 被激活的地址
      activePath:''
    }
  },
  created() {
    this.getMenuList()
    // 被创建的时候就被赋值
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear(); //相当于清空token
      this.$router.push("/login");
    },
    async getMenuList(){
     const {data:res} = await this.$http.get('menus')  //用await，async来简化promise操作，并用解构赋值解析数据
     if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
     this.menulist = res.data
     console.log(res);
    },
    // 绑定地址的点击事件
    saveNavState(activePath){
    window.sessionStorage.setItem('activePath',activePath)
    // 重新赋值
    this.activePath = activePath
  }
  },
};
</script>
<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  margin-left: 15px;
}
.el-aside {
  background-color: #333744;
}
.el-aside .el-menu{
  border-right:none
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right:10px
}
.toggle-color{
  background-color: #4a5064;
  font-size: 10px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
}
</style>